package com.open.ssm.modules.oss.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.open.ssm.common.utils.DateUtils;
import com.open.ssm.common.utils.StringUtils;
import com.open.ssm.modules.oss.IFileAliyunService;
import com.open.ssm.oss.OSSClientUtils;

@Service
public class FileAliyunServiceImpl implements IFileAliyunService {
	
	private Logger logger = LoggerFactory.getLogger(FileAliyunServiceImpl.class);
	
	@Autowired
	private OSSClientUtils ossClientUtils;
	
	// OSS上传文件
	public String uploadFile(String remoteFileName) {
		String ossUrl = "";
		InputStream is = null;
		try {
			// 本地临时文件
			String tempPath = "/mnt/temp/";
			String filePath = tempPath + remoteFileName;
			is = new FileInputStream(filePath);
			String bucketName = ossClientUtils.getBucketName();
			String folder = "aiops/mmbdf-mda/" + DateUtils.parseDateToStr("yyyyMM", new Date()) + "/";
			ossUrl = ossClientUtils.uploadFile(is, bucketName, folder, remoteFileName);
			if(StringUtils.isNotBlank(ossUrl)) {
				// 删除本地临时文件
				new File(filePath).delete();
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {}
		}
		return ossUrl;
	}
	
	// OSS下载文件
	public void downloadFile(String remoteFileName) {
		
	}
}
